package com.xdtech.cc.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.xdtech.sys.aspect.SystemControllerLog;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.cc.model.Account;
import com.xdtech.cc.service.AccountService;
import com.xdtech.cc.service.TradeRecordService;
import com.xdtech.cc.vo.TradeRecordItem;
import com.xdtech.web.controller.BaseController;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2016-05-29 21:53:36
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class TradeRecordController extends BaseController{
	private Log log = LogFactory.getLog(TradeRecordController.class);
	@Autowired
	private TradeRecordService tradeRecordService;
	@Autowired
	private AccountService accountService;
	@RequestMapping(value="/tradeRecord.do",params = "tradeRecord")
	public ModelAndView skipTradeRecord() {
		return new ModelAndView("cc/tradeRecord/tradeRecord_ftl");
	}
	
	@RequestMapping(value="/tradeRecord.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(TradeRecordItem item,Pagination pg) {
		Map<String, Object> results =  tradeRecordService.loadPageDataByConditions(pg,item,"findTradeRecordByCondition");
		return results;
	}
	
	@RequestMapping(value="/tradeRecord.do",params = "editTradeRecord")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editTradeRecord(HttpServletRequest request,Long tradeRecordId) {
		if (tradeRecordId!=null) {
			request.setAttribute("tradeRecordItem", tradeRecordService.loadTradeRecordItem(tradeRecordId));
		}
		List<Account> accounts = accountService.getAll();
		request.setAttribute("accounts", accounts);
		return new ModelAndView("cc/tradeRecord/editTradeRecord_ftl");
	}
	
	@RequestMapping(value="/tradeRecord.do",params = "saveTradeRecord")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveTradeRecord(TradeRecordItem item) {
		ResultMessage r = new ResultMessage();
		if (tradeRecordService.saveOrUpdateTradeRecord(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/tradeRecord.do",params = "deleteTradeRecordItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteTradeRecordItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> tradeRecordIds = new ArrayList<Long>();
			for (String id : tempIds) {
				tradeRecordIds.add(Long.valueOf(id));
			}
			tradeRecordService.deleteTradeRecordInfo(tradeRecordIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
